﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using _9jCardRegisterCollection.Utils;
using AntdUI;
using CardRegisterCollection.Model;

namespace _9jCardRegisterCollection.Views
{
    public partial class UserForm : UserControl
    {

        public UserForm(Window window, User user)
        {
            this._user = user;
            this.window = window;
            InitializeComponent();
            InitData();
        }
        private User _user;
        private Window window;

        /// <summary>
        /// 提交状态
        /// </summary>

        public bool Submit { set; get; }

        private readonly CardRegisterCollection.BLL.Unit _unitBll = new CardRegisterCollection.BLL.Unit();

        private void InitData()
        {
            GetUnit();

            if (_user != null)
            {
                this.txtUserName.Text = _user.UserName;
                this.txtPassword.Text = _user.Password;
                this.txtPhone.Text = _user.Phone;
                this.txtTrueName.Text = _user.TrueName;
                this.cmbUnit.SelectedValue = _user.UnitId;
                this.cmbDept.SelectedValue = _user.DeptId;
            }
        }

        private void GetUnit()
        {
            var ds = _unitBll.GetList(" [Type] = 0 ");
            cmbUnit.Items.Clear();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    cmbUnit.Items.Add(new SelectItem(1)
                    {
                        Tag = row["ID"],
                        Text = row["Name"]?.ToString()
                    });
                }
            }
        }

        /// <summary>
        /// 单位选择项更改加载部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbUnit_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            var value = e.Value;
            GetDeptByParentId(value?.ToString());
        }

        /// <summary>
        /// 通过父编号获取部门
        /// </summary>
        /// <param name="parentId">父编号</param>
        private void GetDeptByParentId(string parentId)
        {
            var ds = _unitBll.GetList($" [ParentId] = '{parentId}' ");
            cmbDept.Items.Clear();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    cmbDept.Items.Add(new SelectItem(1)
                    {
                        Tag = row["ID"],
                        Text = row["Name"]?.ToString()
                    });
                }
            }
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void butCreate_Click(object sender, EventArgs e)
        {
            if (Validation.CheckEmpty(window, this.txtUserName, "请输入账号")
                && Validation.CheckEmpty(window, this.txtPassword, "请输入密码")
                && Validation.CheckEmpty(window, this.txtTrueName, "请输入姓名")
                && Validation.CheckEmpty(window, this.txtPhone, "请输入电话")
                && Validation.CheckEmpty(window, this.cmbUnit, "请选择隶属单位")
                && Validation.CheckEmpty(window, this.cmbDept, "请选择隶属部门"))
            {
                _user.UserName = txtUserName.Text.Trim();
                _user.Password = txtPassword.Text.Trim();
                _user.TrueName = txtTrueName.Text.Trim();
                _user.Phone = txtPhone.Text.Trim();
                _user.UnitId = cmbUnit.SelectedValue?.ToString();
                _user.DeptId = cmbDept.SelectedValue?.ToString();

                Submit = true;
                this.Dispose();
            }
        }
    }
}
